﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;

namespace COER.Manual
{
    public partial class UC_LoadCVCtr : UserControl
    {
        private int CVIndex = 0;
        private string Name = "";
        public UC_LoadCVCtr()
        {
            InitializeComponent();
            GetControls(this.Controls);
        }

        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                //if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                //{
                //    ((Button)item).OP = Kernel.OP;
                //}

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        public void Init(int Idx,string name)
        {
            CVIndex = Idx;
            Name = name;

        }
        public void Load()
        {
            this.groupBox1.Text = Name;
            timer1.Start();
        }
        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');

            switch (info[1])
            {
                #region 轨道切换
                case "RecvPos1":
                    {
                        if (CVIndex==1)
                        {
                            ProcedureHelper_Exc.CV1_TrackSW_Y_ToLDPt1 = true;
                        }
                        else if (CVIndex==2)
                        {
                            ProcedureHelper_Exc.CV2_TrackSW_Y_ToLDPt1 = true;
                        }
                        
                    }
                    break;
                case "RecvPos2":
                    {                       
                        if (CVIndex == 1)
                        {
                            ProcedureHelper_Exc.CV1_TrackSW_Y_ToLDPt2 = true;
                        }
                        else if (CVIndex == 2)
                        {
                            ProcedureHelper_Exc.CV2_TrackSW_Y_ToLDPt2 = true;
                        }
                    }
                    break;
                case "ToInsp1":
                    {
                        if (CVIndex == 1)
                        {
                            ProcedureHelper_Exc.CV1_TrackSW_Y_ToInsp1 = true;
                        }
                        else if (CVIndex == 2)
                        {
                            ProcedureHelper_Exc.CV2_TrackSW_Y_ToInsp1 = true;
                        }                                           
                    }
                    break;
                case "ToInsp2":
                    {
                        if (CVIndex == 1)
                        {
                            ProcedureHelper_Exc.CV1_TrackSW_Y_ToInsp2 = true;
                        }
                        else if (CVIndex == 2)
                        {
                            ProcedureHelper_Exc.CV2_TrackSW_Y_ToInsp2 = true;
                        }                    
                    }
                    break;
                case "Insp1":
                    {
                        ProcedureHelper_Exc.Insp1_RM_Z_ToWork =true; 
                    }
                    break;

                #endregion

                #region  CV控制
                case "CVRun":
                    {
                        if (CVIndex == 1)
                        {
                            ProcedureHelper_Exc.CV1_ToRun = true;
                        }
                        else if (CVIndex == 2)
                        {
                            ProcedureHelper_Exc.CV2_ToRun = true;
                        }
                    }
                    break;
                case "CVStop":
                    {
                        if (CVIndex == 1)
                        {
                            ProcedureHelper_Exc.CV1_ToStop = true;
                        }
                        else if (CVIndex == 2)
                        {
                            ProcedureHelper_Exc.CV2_ToStop = true;
                        }
                    }
                    break;
                #endregion

                #region CV定位Y轴控制
                case "CV_Y_Work":
                    {
                        if (CVIndex == 1)
                        {
                            ProcedureHelper_Exc.CV1_Y_ToWork = true;
                        }
                        else if (CVIndex == 2)
                        {
                            ProcedureHelper_Exc.CV2_Y_ToWork = true;
                        }
                    }
                    break;
                case "CV_Y_Wait":
                    {
                        if (CVIndex == 1)
                        {
                            ProcedureHelper_Exc.CV1_Y_ToWait = true;
                        }
                        else if (CVIndex == 2)
                        {
                            ProcedureHelper_Exc.CV2_Y_ToWait = true;
                        }
                    }
                    break;
                #endregion

                #region  气缸控制
                case "CylUp":
                    {
                        Cyl_Ctr_Up(CVIndex);
                    }
                    break;
                case "CylDn":
                    {
                        Cyl_Ctr_Dn(CVIndex);
                    }
                    break;
                #endregion
                case "IDRead":
                    {                        
                        if (CVIndex == 1)
                        {
                            Kernel.codeRead1.Code = " ";
                            Kernel.codeRead1.Sending = true;
                        }
                        else if (CVIndex == 2)
                        {
                            Kernel.codeRead2.Code = " ";
                            Kernel.codeRead2.Sending = true;
                        }
                    }
                    break;
                    
            }
        }

        private void Cyl_Ctr_Up(int index)
        {
            if (index == 1)
            {
                ProcedureHelper_Exc.CV1_Cyl_ToUp = true;
            }
            else
            {
                ProcedureHelper_Exc.CV2_Cyl_ToUp = true;
            }

        }
        private void Cyl_Ctr_Dn(int index)
        {
            if (index == 1)
            {
                ProcedureHelper_Exc.CV1_Cyl_ToDn = true;
            }
            else
            {
                ProcedureHelper_Exc.CV2_Cyl_ToDn = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            FreshStatus();
        }
        private void FreshStatus()
        {
            if (CVIndex == 1)
            {
                btn_CVRun.BackColor = ProcedureHelper_Exc.CV1_Run_BL ? Color.Green : Color.LightGray;
                btn_CVStop.BackColor = ProcedureHelper_Exc.CV1_Stop_BL ? Color.Green : Color.LightGray;
                btn_CV_Y_Wait.BackColor = ProcedureHelper_Exc.CV1_Y_Wait_BL ? Color.Green : Color.LightGray;
                btn_CV_Y_Work.BackColor = ProcedureHelper_Exc.CV1_Y_Work_BL ? Color.Green : Color.LightGray;
                btn_CylDn.BackColor = ProcedureHelper_Exc.CV1_Cyl_Dn_BL ? Color.Green : Color.LightGray;
                btn_CylUp.BackColor = ProcedureHelper_Exc.CV1_Cyl_Up_BL ? Color.Green : Color.LightGray;
                btn_RecvPos1.BackColor = ProcedureHelper_Exc.CV1_TrackSW_Y_LDPt1_BL ? Color.Green : Color.LightGray;
                btn_RecvPos2.BackColor = ProcedureHelper_Exc.CV1_TrackSW_Y_LDPt2_BL ? Color.Green : Color.LightGray;
                btn_ToInsp1.BackColor = ProcedureHelper_Exc.CV1_TrackSW_Y_Insp1_BL ? Color.Green : Color.LightGray;
                btn_ToInsp2.BackColor = ProcedureHelper_Exc.CV1_TrackSW_Y_Insp2_BL ? Color.Green : Color.LightGray;
                ID.Text=Kernel.codeRead1.Code.ToString();
            }
            else if (CVIndex == 2)
            {
                btn_CVRun.BackColor  = ProcedureHelper_Exc.CV2_Run_BL ? Color.Green : Color.LightGray;
                btn_CVStop.BackColor = ProcedureHelper_Exc.CV2_Stop_BL ? Color.Green : Color.LightGray;
                btn_CV_Y_Wait.BackColor = ProcedureHelper_Exc.CV2_Y_Wait_BL ? Color.Green : Color.LightGray;
                btn_CV_Y_Work.BackColor = ProcedureHelper_Exc.CV2_Y_Work_BL ? Color.Green : Color.LightGray;
                btn_CylDn.BackColor = ProcedureHelper_Exc.CV2_Cyl_Dn_BL ? Color.Green : Color.LightGray;
                btn_CylUp.BackColor = ProcedureHelper_Exc.CV2_Cyl_Up_BL ? Color.Green : Color.LightGray;
                btn_RecvPos1.BackColor = ProcedureHelper_Exc.CV2_TrackSW_Y_LDPt1_BL ? Color.Green : Color.LightGray;
                btn_RecvPos2.BackColor = ProcedureHelper_Exc.CV2_TrackSW_Y_LDPt2_BL ? Color.Green : Color.LightGray;
                btn_ToInsp1.BackColor = ProcedureHelper_Exc.CV2_TrackSW_Y_Insp1_BL ? Color.Green : Color.LightGray;
                btn_ToInsp2.BackColor = ProcedureHelper_Exc.CV2_TrackSW_Y_Insp2_BL ? Color.Green : Color.LightGray;
                ID.Text = Kernel.codeRead2.Code.ToString();
            }
        }
    }
}
